﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hxj.Data;
using System.Data;
using MsgCenter.DAL.Base;
using Utility;
using Utility.Model;

namespace MsgCenter.BLL.Impl
{
    /// <summary>
    /// 字典管理业务层
    /// </summary>
    public class BLL_SysDic
    {
        private readonly BaseService dal = new BaseService();
        /// <summary>
        /// 获取最大的排序号
        /// </summary>
        /// <param name="strTableName"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        private int GetMaxSort(string strTableName, string strWhere)
        {
            string strSql = "select ISNULL(Max(Sort),0)+1 from " + strTableName + "";
            if (strWhere.Length > 0)
            {
                strSql += " where " + strWhere;
            }
            return dal.ToScalar<int>(strSql);
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="model">对象实体</param>
        public void Save(Sys_Dic model)
        {
            if (model.IsNewModel)
            {
                if (model.ParentDicID.IsNullOrEmpty())
                {
                    model.ParentDicID = Guid.Empty.ToString();
                }
                model.Sort = GetMaxSort(model.GetTableName(), "ParentDicID='" + model.ParentDicID + "'");
                if (model.ParentDicID == Guid.Empty.ToString())
                {
                    string strDicID = dal.ToScalar<string>("select ISNULL(Max(dicID),0)+10000 from Sys_DIc where ParentDicID='00000000-0000-0000-0000-000000000000'");
                    strDicID = strDicID.PadLeft(6, '0');
                    model.DicID = strDicID;
                }
                else
                {
                    int strDicID = dal.ToScalar<int>("select ISNULL(Max(dicID),0) from Sys_DIc where ParentDicID='" + model.ParentDicID + "'");
                    if (strDicID == 0)
                    {
                        strDicID = 100;
                        model.DicID = (model.ParentDicID.Substring(0, 3) + strDicID.ToString()).ToString();
                    }
                    else
                    {
                        strDicID = strDicID + 100;
                        model.DicID = strDicID.ToString().PadLeft(6, '0');
                    }
                }
            }
            DbTrans trans = dal.GetDataBase().BeginTransaction();
            try
            {
                dal.Save<Sys_Dic>(model, trans);
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="strDataID">数据主键ID</param>
        public void DeleteDic(string strDataID)
        {
            string[] idList = strDataID.Trim(',').Split(',');
            DbTrans trans = dal.GetDataBase().BeginTransaction();
            try
            {
                foreach (string strOperateID in idList)
                {
                    dal.ExecuteNonQuery("Update Sys_Dic set Isdelete=1 where DicID='" + strOperateID + "'", trans);
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
        /// <summary>
        /// 获取对象实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public Sys_Dic GetModelDic(string id)
        {
            return dal.GetModel<Sys_Dic>(Sys_Dic._.DicID == id);
        }
        /// <summary>
        /// 获取对象Table
        /// </summary>
        /// <param name="wcp"></param>
        /// <returns></returns>
        public DataTable GetDicTable(WhereClip wcp)
        {
            return dal.GetTable<Sys_Dic>(wcp);
        }
        /// <summary>
        /// 获取对象Table
        /// </summary>
        /// <param name="wcp"></param>
        /// <returns></returns>
        public DataTable GetDicTable(string strPartentID)
        {
            return GetDicTable(Sys_Dic._.ParentDicID == strPartentID && Sys_Dic._.IsDelete == false);
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="strSelectValue"></param>
        public void Update_Sort(string strSelectValue)
        {
            string[] strData = strSelectValue.Split(',');
            DbTrans trans = dal.GetDataBase().BeginTransaction();
            try
            {
                for (int i = 0; i < strData.Length; i++)
                {
                    dal.ExecuteNonQuery(string.Format("update {0} set sort={1} where DicID='{2}'", "Sys_Dic", i + 1, strData[i].ToString()), trans);
                }
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Close();
            }
        }
    }
}
